<script setup lang="ts">
import logo from "@/assets/login/logo.png";
import { ReImageVerify } from "@/components/ReImageVerify/index";
import type { FormInstance } from "element-plus";
import { useUserStoreHook } from "@/store/modules/user";
import { useRouter } from "vue-router";
import { initRouter, getTopMenu } from "@/router/utils";
const router = useRouter();

defineOptions({
	name: 'login'
});

// 状态
const loading = ref(false);
const imgCode = ref("");
const ruleFormRef = ref<FormInstance>();
// 表单数据
const form = reactive({
	// username: storage.get('username') || '',
	username: 'admin',
	password: 'admin123',
	captchaId: '',
	verifyCode: ''
});

// 登录
async function onLogin (formEl: FormInstance | undefined) {
  if (!formEl) return;
	try {
    await formEl.validate(valid => {
      if (valid) {
        loading.value = true;
        useUserStoreHook()
        .loginByUsername({
          username: form.username,
          password: form.password
        })
        .then(res => {
          // 获取后端路由
            return initRouter().then(() => {
              disabled.value = true;
              router
                .push(getTopMenu(true).path)
                .then(() => {
                  message(t("login.pureLoginSuccess"), { type: "success" });
                })
                .finally(() => (loading.value = false));
            });
        })
      }
    }).finally(() => (loading.value = false));
	} catch (err) {
		// 刷新验证码
		// refs.picCaptcha.refresh();

		// 提示错误
		// ElMessageBox.alert((err as Error).message, {
		// 	title: t('提示'),
		// 	type: 'error'
		// });
	}

	loading.value = false;
}

watch(imgCode, value => {
  useUserStoreHook().SET_VERIFYCODE(value);
});
</script>

<template>
  <div class="login-page">
    <!-- 登录表单 -->
    <div class="container">
      <div class="logo">
        <div class="icon">
          <img :src="logo" class="wave" alt="logo" />
        </div>
        <div class="name">
          <span>B</span>
          <span>I</span>
          <span>R</span>
          <span>D</span>
          <span>-</span>
          <span>A</span>
          <span>D</span>
          <span>M</span>
          <span>I</span>
          <span>N</span>
        </div>
      </div>
      <p class="desc">快速开发后台权限管理系统</p>
      <div class="form">
        <el-form :model="form" ref="ruleFormRef" label-position="top" class="form" :disabled="loading">
          <el-form-item label="账号">
              <el-input
                v-model="form.username"
                placeholder="请输入账号"
                maxlength="20"
              />
            </el-form-item>
            <el-form-item label="密码">
              <el-input
                v-model="form.password"
                type="password"
                placeholder="请输入密码"
                maxlength="20"
                show-password
                autocomplete="new-password"
              />
            </el-form-item>
            <el-form-item label="验证码">
						<el-input
							v-model="form.verifyCode"
							placeholder="验证码"
							maxlength="4"
							@keyup.enter="onLogin"
						>
							<template v-slot:append>
                <ReImageVerify v-model:code="imgCode" />
              </template>
						</el-input>
					</el-form-item>
            <div class="op">
						<el-button type="primary" :loading="loading" @click="onLogin(ruleFormRef)">
							登录
						</el-button>
					</div>
        </el-form>
      </div>
    </div>
    <!-- 动态背景 -->
    <ul class="bg-squares">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
  </div>
</template>

<style lang="scss" scoped>
@import url("./index.scss");
</style>